package com.epfresh.supplier.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.api.utils.FormatUtil;
import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.Goods;

import java.math.BigDecimal;

import multitype.ItemViewBinder;
import multitype.OnRecyclerViewItemClickListener;


public class GoodsStockListViewBinder extends ItemViewBinder<Goods, GoodsStockListViewBinder.ViewHolder> implements View.OnClickListener {

    OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;
    OnStockListener onStockListener;

    public OnStockListener getOnStockListener() {
        return onStockListener;
    }

    public void setOnStockListener(OnStockListener OnStockListener) {
        this.onStockListener = OnStockListener;
    }

    public OnRecyclerViewItemClickListener getOnRecyclerViewItemClickListener() {
        return onRecyclerViewItemClickListener;
    }

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }

    //更新某个位置的数据
    public void notifyItem(int position) {
        if (position > -1 && position < getAdapter().getItemCount())
            getAdapter().notifyItemChanged(position);
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(
            @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_category_goods_stock, parent, false);
        return new ViewHolder(view);
    }


    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull Goods post) {

        int position = getPosition(holder);

        holder.tvStock.setTag(R.id.item_key_position, position);
        holder.tvStock.setTag(R.id.item_key_status, post);
        holder.tvStock.setOnClickListener(this);

        int itemCount = getAdapter().getItemCount();

        holder.setData(post);
        if (itemCount -2 == position){
            holder.vUpdateBg.setVisibility(View.GONE);
        }else {
            holder.vUpdateBg.setVisibility(View.VISIBLE);
        }


    }

    @Override
    public void onClick(View v) {

        Object tag = v.getTag(R.id.item_key_position);
        if (tag != null && tag instanceof Integer){
            int position = (int) tag;
            Object o = v.getTag(R.id.item_key_status);

          if (v.getId() == R.id.tv_item_stock) {
              if (onStockListener != null) {
                  onStockListener.onStock(position, (Goods) o);
              }

          } else if (onRecyclerViewItemClickListener != null && o instanceof Goods) {
              onRecyclerViewItemClickListener.onItemClick(v, position, o);
          }
        }
    }
    static class ViewHolder extends RecyclerView.ViewHolder {


        ImageView iv;
        TextView tvTitle;
        TextView tvPackage;
        TextView tvInventory;
        //TextView tvUnit;
        TextView tvStock;

        TextView tv_sale_price_title;

        View vUpdateBg;



        ViewHolder(@NonNull View itemView) {
            super(itemView);


            iv = (ImageView) itemView.findViewById(R.id.iv_info_img);

            tv_sale_price_title = (TextView) itemView.findViewById(R.id.tv_sale_price_title);

            tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
            tvPackage = (TextView) itemView.findViewById(R.id.tv_package);
            tvInventory = (TextView) itemView.findViewById(R.id.tv_inventory);
            tvStock = (TextView) itemView.findViewById(R.id.tv_item_stock);
            vUpdateBg = itemView.findViewById(R.id.v_update);

        }
        void setData(Goods post) {
            String thumbnail = post.getThumbnail();
            ImageLoader.getInstance().displayImage(thumbnail, iv);
            tvTitle.setText(post.getTitle());
            tvPackage.setText(post.getPackStr());
            BigDecimal bigDecimal = new BigDecimal( post.getInventory());
            tvInventory.setText("库存"+FormatUtil.subZeroAndDot(bigDecimal.toString()));
        }
    }

    public interface OnStockListener {
        void onStock(int position, Goods goods);
    }
}
